package com.ymatou.iapi.exp.testcase;

import java.io.IOException;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.exp.parameter.AddUserCoinByEvalBean;
import com.ymatou.iapi.exp.parameter.GetUserEvalCoinBean;
import com.ymatou.iapi.exp.service.AddUserCoinByEvalCall;
import com.ymatou.iapi.exp.service.GetUserEvalCoinCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
/**
*获取用户评价已赚金币数
*FreeMaker Template自动生成代码
*/
import com.ymttest.business.service.ExpCallService;
@TestSuite
public class Ts_GetUserEvalCoin{
	private static GetUserEvalCoinBean getuserevalcoinBean;
	private static GetUserEvalCoinCall getuserevalcoinCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户评价已赚金币数");
	}

	@Before
	public void caseUp() {
		getuserevalcoinBean=new GetUserEvalCoinBean();
		getuserevalcoinCall=new GetUserEvalCoinCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuserevalcoinCall.getOpurl());
	}
	/**
	 * 给用户添加评价金币 一次5个
	 * @param userId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private void AddUserEvalCoin(int userId) throws ClientProtocolException, IOException{
		AddUserCoinByEvalBean addusercoinbyevalBean=new AddUserCoinByEvalBean();
		AddUserCoinByEvalCall addusercoinbyevalCall=new AddUserCoinByEvalCall();
		addusercoinbyevalBean = ExpCallService.initAddusercoinbyevalBean(userId);
		addusercoinbyevalCall.setData(addusercoinbyevalBean);
		addusercoinbyevalCall.callService();
	}
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserEvalCoin_001() {
		Logger.start(true,"获取用户评价已赚金币数成功");
		try {
			int userId=9001;
			ExpCallService.resetCoinAccount(userId);
			AddUserEvalCoin(userId);
			getuserevalcoinBean.setUserId(userId);
			getuserevalcoinCall.setData(getuserevalcoinBean);
			getuserevalcoinCall.callService();		
			JsonObject result = getuserevalcoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getuserevalcoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(ExpCallService.EvalCoin, result.get("coin").getAsInt(), "验证coin");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserEvalCoin_002() {
		Logger.start(true,"不输入userid参数失败");
		try {
			getuserevalcoinCall.setData(getuserevalcoinBean);
			getuserevalcoinCall.callService();			
			JsonObject result = getuserevalcoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("false", result.get("success").toString(), "验证success");
			Logger.verifyEquals("2", getuserevalcoinCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserEvalCoin_003() {
		Logger.start(true,"用户评价多次获取用户评价已赚金币数成功");
		try {
			int userId=9003;
			ExpCallService.resetCoinAccount(userId);
			int count=new Random().nextInt(10)+1;
			for (int i = 0; i < count; i++) {
				AddUserEvalCoin(userId);
			}
			getuserevalcoinBean.setUserId(userId);
			getuserevalcoinCall.setData(getuserevalcoinBean);
			getuserevalcoinCall.callService();		
			JsonObject result = getuserevalcoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getuserevalcoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(count*ExpCallService.EvalCoin, result.get("coin").getAsInt(), "验证coin");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}